`
https://leetcode.cn/problems/swap-nodes-in-pairs/
`

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var swapPairs = function (head) {
  if (!head || !head.next) return head
  const dummy = {
    next: head
  }
  let pre = dummy
  while (pre.next && pre.next.next) {
    let left = pre.next
    let right = pre.next.next
    // 交换
    swap(left, right);
    [left, right] = [right, left]
    pre.next = left
    // 步进
    pre = pre.next.next
  }
  return dummy.next
};

function swap(node1, node2) {
  node1.next = node2.next
  node2.next = node1
}